MODULE MPI_DEBUG_TOOLS

CONTAINS

SUBROUTINE WRITE_ARRAY(array,nxi,nxe,nyi,nye,nsi,nse)

implicit none

! Input variables

real(8), intent(in) :: array(nxi:nxe,nyi:nye,nsi:nse)

integer, intent(in) :: nxi, nxe
integer, intent(in) :: nyi, nye
integer, intent(in) :: nsi, nse

! Local variables

integer, save :: counter
integer       :: outunit 
integer       :: i, j, k ! Loop indices

character*10  :: outfile
character*1   :: identifier
logical       :: connected

data counter /0/

counter = counter + 1
outunit = 10000
call CHECK_UNIT0(outunit)
identifier = 'y'

write(outfile,'(i10)') counter
outfile = identifier//adjustl(outfile)

open  (unit = outunit, file = outfile, status = 'unknown')
do k = nsi, nse
  do j = nyi, nye
    do i = nxi, nxe
      write (outunit, '(3i6,f20.15)') i, j, k, array(i,j,k)
    enddo
  enddo
enddo
!write (outunit, '(3i6,f)') ((( (i, j, k, array(i,j,k)), i=nxi,nxe), j=nyi,nye), k=nsi,nse)
close (outunit)

END SUBROUTINE WRITE_ARRAY
END MODULE MPI_DEBUG_TOOLS
